<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Vendor\ThinkImage\ThinkImage;

class MemberController extends AuthController {

    public function index(){

        $key=I('key');

        if($key&&$key!=""){
            $map = array(
                'truename' =>array('like', "%".$key."%"),
                'mobile'=>array('like', "%".$key."%"),                
                '_logic'     =>'or',
            );
            
        }

        $sldate=urldecode(I('reservation',''));//获取格式 2015-11-12 - 2015-11-18
        $arr = explode(" - ",$sldate);//转换成数组
        $arrdateone=strtotime($arr[0]);
        $arrdatetwo=strtotime($arr[1].' 23:55:55');  
        //var_dump($arrdateone);exit;
        if($arrdateone && $arrdatetwo){
            $map['ctime'] = array(array('egt',$arrdateone),array('elt',$arrdatetwo),'AND');
        }  

        $Nowpage = $_GET['p']?$_GET['p']:1;
        $limits = 5;// 获取总条数
        $count = M('user')->where($map)->count();//计算总页面
        $allpage = ceil($count / $limits);
        $allpage = intval($allpage);
        $lists = M('user')->where($map)->page($Nowpage, $limits)->order('ctime desc')->select();
        foreach($lists as $k=>$v)
        {          
            $lists[$k]['ctime']=date('Y年m月d日',$v['ctime']);
        }


         $p = getpage($count,$limits);
         //$p = getpage($count,C('PAGE_SIZE'));
         $show = $p->show(); 
        $this->assign('page', $show);
        $this->assign('Nowpage', $Nowpage);
        $this->assign('lists', $lists);
        $this->assign('allpage', $allpage);
        $this->assign('s',$sldate);
        $this->assign('val',$key);
        //var_dump($_GET['page']);exit;
        if($_GET['page']){
        $this->success($lists);
        }
        $this->display();
    }
	

    public function option()
    {   
       $Model= M('options as o');
       $lists=$Model->join('JOIN lh_user as u ON o.user_id = u.id')->field('o.id as optionid,o.add_time,o.content,u.mobile as mobile,u.truename as truename,u.idcard as idcard')->order('o.add_time DESC')->select();


         $this->assign('lists', $lists);
        $this->display();
    }

    public function editOption($id= 0)
    {
        if ($id = (int)$id) {
            $obj = D('options');
            if (!$detail = $obj -> find($id)) {
                $this -> error('请选择要标记的会员意见');
            }
                $data['id'] = $id;
                $data['status'] =1;

                if ($obj -> save($data)) {
                    $this -> success('操作成功');
                } else {
                    $this -> error('操作失败');
                }

        } else {
            $this -> error('请选择要标记的会员意见');
        }
    }


    public function delOption()
    {  $id=I('id');
      $del= M('options')->delete($id);
      if ($del) {
         exit('1');
      }
    
    }

    public function removesj()
    {   
        $lists=M("jb_records")->select();
        $this->assign('lists',$lists);
        $this->display();
    }
    //解绑审核
       public function editjb($id= 0)
    {
        if ($id = (int)$id) {
            $obj = D('jb_records');
            if (!$detail = $obj -> find($id)) {
                $this -> error('请选择要标记的会员意见');
            }
                $data['id'] = $id;
                $data['jb_status'] =1;

                if ($obj -> save($data)) {
                    $this -> success('操作成功');
                } else {
                    $this -> error('操作失败');
                }

        } else {
            $this -> error('请选择要标记的会员意见');
        }
    }

    public function edit()
    {       

        $lists=M("jb_records")->select();
        $this->assign('lists',$lists);
        $this->display();
    }

    public function deluser()
    {   $id=I('post.id');
        $del=M('user')->delete($id);
        if ($del) {
            echo "1";
        }
    }


     public function frozen()
    {   $data['id']=I('post.id');
       $data['user_frozen']=1;
        $del=M('user')->save($data);
        if ($del) {
            echo "1";
        }
    }

     public function rm_frozen()
    {    $data['id']=I('post.id');
       $data['user_frozen']=0;
        $del=M('user')->save($data);
        if ($del) {
            echo "1";
        }
    }

    public function tree()
    {   
       
         $sldate=urldecode(I('reservation',''));//获取格式 2015-11-12 - 2015-11-18
        $this->display();
    }
    //添加会员
       public function user_list_add(){
        
        if (!IS_AJAX){
            $jibie=M('jbset')->find();
            $this->assign('jibie',$jibie);
            $auth_group=M('auth_group_home')->select();
            $this->assign('auth_group',$auth_group);
        $this->display();
        }else{
            $user=M('user');
            $user_access=M('auth_group_access_home');

            $check_user=$user->where(array('mobile'=>I('mobile')))->find();
            $check_user_id=$user->where(array('idcard'=>I('idcard')))->find();
            if ($check_user){
                $this->error('手机号已存在，请重新输入手机号',0,0);
            }
            if ($check_user_id){
                $this->error('用户身份证已存在，请重新输入身份证号',0,0);
            }
            $sldata=array(
                'truename'=>I('truename'),
                'password'=>I('password','','md5'),
                'idcard'=>I('idcard'),
                'mobile'=>I('mobile'),
                'user_address'=>I('address'),
                'invite_id'=>I('invite_id'),
                'f_id'=>I('invite_id'),
                'status'=>I('status',1),
                'ip'=>get_client_ip(),
                'ctime'=>time(),
            );

            $result=$user->add($sldata);
            $A=A('Home/login');
            $A->info($result);

            $accdata=array(
                'uid'=>$result,
                'group_id'=>I('group_id'),
            );
            $user_access->add($accdata);

            $this->success('用户添加成功',U('index'),1);
        }  
        
    }

    //编辑会员
    public function user_list_edit(){
            
        if (!IS_POST){
            $auth_group=M('auth_group_home')->select();
            $admin_list=M('user')->where(array('id'=>I('userid')))->find();
            $jibie=M('jbset')->find();
            $this->assign('jibie',$jibie);
            $auth_group_access=M('auth_group_access_home')->where(array('uid'=>$admin_list['id']))->getField('group_id');
            $this->assign('admin_list',$admin_list);
            $this->assign('auth_group',$auth_group);
            $this->assign('auth_group_access',$auth_group_access);
            $this->display();
        }else{
            $admin_list=M('user');
            $admin_pwd=I('password');
            $group_id=I('group_id');
            $admindata['id']=I('user_id');
            if ($admin_pwd){
                $admindata['password']=I('password','','md5');
                $admindata['password_show']=I('password');
            }
            $admindata['mobile']=I('mobile');
            $admindata['truename']=I('truename');
            $admindata['address']=I('address');
            $admindata['status']=I('status',1);
            $admindata['idcard']=I('idcard');
            $admin_list->save($admindata);
            //修改用户组
            M('auth_group_access_home')->where(array('uid'=>I('user_id')))->setField('group_id',$group_id);
            $this->success('用户修改成功',U('index'),1);
        }
                    
    }


}